package com.oocl.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.vo.MerchandiseVO;

/**
 * @description: 
 * @author  XIEMI2
 * @date Aug 4, 2017 4:49:22 AM
 * @version 1.0
 */
public class JsonUtil2 {
	private static ObjectMapper mapper = new ObjectMapper();

	public static String parserToString(MerchandiseVO MerchandiseVO) throws JsonProcessingException {
		String jsonStr = null;
		jsonStr = mapper.writeValueAsString(MerchandiseVO);

		return jsonStr;
	}

	public static MerchandiseVO parserToDTO(String json) throws JsonParseException, JsonMappingException, IOException {
		MerchandiseVO MerchandiseVO = null;
		MerchandiseVO = mapper.readValue(json, MerchandiseVO.class);
		return MerchandiseVO;
	}

	public static List<MerchandiseVO> parserToDTOs(String json) throws JsonParseException, JsonMappingException, IOException{
		List<MerchandiseVO> MerchandiseVOs = new ArrayList<MerchandiseVO>();
		JavaType javaType = getCollectionType(ArrayList.class, MerchandiseVO.class); 
		MerchandiseVOs = mapper.readValue(json, javaType);
		return MerchandiseVOs;
	}

	
	 /**   
     * 获取泛型的Collection Type  
     * @param collectionClass 泛型的Collection   
     * @param elementClasses 元素类   
     * @return JavaType Java类型   
     * @since 1.0   
     */   
	 public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {   
	     return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);   
	 }
}


